"use client";
import { trpc } from "@/components/trpc-provider";
import { Button } from "@/components/ui/button";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";

const formSchema = z.object({
  // id 必填
  path: z.string().min(1, { message: "请填写path" }),
});

export default function Blog() {
  const revalidateMutation = trpc.page.revalidatePath.useMutation();
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      path: "/blog/caip1",
    },
  });

  function onSubmit({ path }: z.infer<typeof formSchema>) {
    console.log("data", path);
    revalidateMutation.mutate(
      {
        path,
      },
      {
        onSuccess: (data) => {
          console.log("r", data.msg);
        },
      }
    );
  }

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
        <FormField
          control={form.control}
          name="path"
          render={({ field }) => (
            <FormItem>
              <FormLabel>博客id</FormLabel>
              <FormControl>
                {/* 数字输入框 */}
                <Input placeholder="这是清除缓存的博客id" {...field} />
              </FormControl>
              <FormDescription>清除指定博客id的缓存</FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <Button type="submit">清除缓存</Button>
      </form>
    </Form>
  );
}
